package com.caiwm.client.view;

import com.caiwm.client.common.MessageEntity;
import com.caiwm.client.common.MessageType;
import com.caiwm.client.common.Utils;
import com.caiwm.client.protocol.TCPUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * <pre>
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃  神兽保佑
 * 　　　　┃　　　┃  代码无bug
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 * </pre>
 *
 * @author shiqla
 * @Desc ${DESC}
 * @Date 2019/1/11/011
 */
public class ReivceThread implements Runnable {

    private ChatView chatView;

    public ReivceThread(ChatView chatView) {
        this.chatView = chatView;
    }

    public void run() {
        while (true) {
            final MessageEntity messageEntity = TCPUtils.getInstance().reivceMessage();
            if (messageEntity.getHead().equals(MessageType.message)) {
                //如果是广播消息，则需要显示在聊天框中..

                StringBuilder stringBuilder = new StringBuilder();
                String message = this.chatView.getMessageTextArea().getText();
                stringBuilder.append(message + "\n");
                stringBuilder.append(messageEntity.getUsername() + ":\n");
                stringBuilder.append(messageEntity.getBody() + "\n");
                this.chatView.getMessageTextArea().setText(stringBuilder.toString());

            } else if (messageEntity.getHead().equals(MessageType.online)) {

                //用户上线消息
                JButton jButton = new JButton(messageEntity.getUsername());

                jButton.setBackground(new Color(103, 86, 228));

                jButton.addActionListener(new ActionListener() {
                    //所有的在线用户绑定事件,单击用户名称可以进入私聊界面

                    private ChatView chatView;
                    private JButton jButton;


                    public void actionPerformed(ActionEvent e) {

                        jButton.setBackground(new Color(103, 86, 228));
                        String toUserName = e.getActionCommand();

                        PrivateChatView privateChatView = new PrivateChatView(toUserName, this.chatView.getUsername(), this.chatView);

                        this.chatView.setOpenPrivateWindow(true);

                        //打开聊天窗口之前,组织已有的消息
                        StringBuilder stringBuilder = new StringBuilder();

                        ArrayList<MessageEntity> arrayList = Utils.getMessageList(toUserName);
                        if (arrayList != null) {
                            for (MessageEntity entity : arrayList) {
                                stringBuilder.append(toUserName + ":\n");
                                stringBuilder.append("  " + entity.getBody() + "\n");
                            }
                            //删除队列里面的消息
                            Utils.clearMessagetByUserName(toUserName);
                        }
                        privateChatView.start(stringBuilder.toString());
                        this.chatView.getPrivateChatViewList().add(privateChatView);
                    }

                    ActionListener init(ChatView chatView,JButton jButton) {
                        this.chatView = chatView;
                        this.jButton = jButton;
                        return this;
                    }
                }.init(this.chatView,jButton));

                synchronized ("lock") {
                    this.chatView.getLeftPanel().add(jButton);
                    this.chatView.getList().add(jButton);
                    this.chatView.getLeftPanel().validate();
                }

                StringBuilder stringBuilder = new StringBuilder();
                String message = this.chatView.getMessageTextArea().getText();
                stringBuilder.append(message + "\n");
                stringBuilder.append("用户： " + messageEntity.getUsername() + " 上线。\n");
                this.chatView.getMessageTextArea().setText(stringBuilder.toString());
            } else if (messageEntity.getHead().equals(MessageType.offline)) {
                //用户下线消息消息处理
                StringBuilder stringBuilder = new StringBuilder();
                String message = this.chatView.getMessageTextArea().getText();
                stringBuilder.append(message + "\n");
                stringBuilder.append("用户： " + messageEntity.getUsername() + " 下线！\n");
                this.chatView.getMessageTextArea().setText(stringBuilder.toString());

                JButton jButton = null;
                String username = messageEntity.getUsername();
                for (JComponent jComponent : this.chatView.getList()) {
                    if (((JButton) jComponent).getText().equals(username)) {
                        jButton = (JButton) jComponent;
                        break;
                    }
                }
                synchronized ("lock") {
                    this.chatView.getLeftPanel().remove(jButton);
                    this.chatView.getList().remove(jButton);
                    this.chatView.getLeftPanel().repaint();
                }

            } else if (messageEntity.getHead().equals(MessageType.privateMessage)) {
                //如果是私聊消息,处理逻辑
                String username = messageEntity.getUsername();
                boolean isHave = false;
                for (PrivateChatView privateChatView : this.chatView.getPrivateChatViewList()) {
                    if (privateChatView.getToUserName().equals(username)) {
                        String message = privateChatView.getShowMessageTextArea().getText();
                        StringBuilder stringBuilder = new StringBuilder(message);
                        stringBuilder.append(privateChatView.getToUserName() + ":\n");
                        stringBuilder.append("      " + messageEntity.getBody() + "\n");
                        privateChatView.getShowMessageTextArea().setText(stringBuilder.toString());
                        isHave = true;
                        break;
                    }
                }

                if (!isHave) {
                    //如果当前的窗口没有打开,则改变目标窗口的颜色,并把接受到的消息放入队列中
                    Utils.put(username, messageEntity);
                    for (JComponent jComponent : this.chatView.getList()) {
                        JButton jButton = (JButton) jComponent;
                        if (jButton.getText().equals(username)) {
                            try {
                                Utils.componentFlicker(jButton, 5, 100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            this.chatView.getMessageTextArea().setCaretPosition(
                    this.chatView.getMessageTextArea().getDocument().getLength());
        }
    }
}
